package code;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.sound.sampled.Line;

import utils.SystemParas;
import Function.Function;

import com.sun.jna.Library;
import com.sun.jna.Native;

public class NlpirTest {

	// 定义接口CLibrary，继承自com.sun.jna.Library
	public interface CLibrary extends Library {
		// 定义并初始化接口的静态变量
		CLibrary Instance = (CLibrary) Native.loadLibrary(
				"NLPIR", CLibrary.class);
		
		public int NLPIR_Init(String sDataPath, int encoding,
				String sLicenceCode);
				
		public String NLPIR_ParagraphProcess(String sSrc, int bPOSTagged);

		public String NLPIR_GetKeyWords(String sLine, int nMaxKeyLimit,
				boolean bWeightOut);
		public String NLPIR_GetFileKeyWords(String sLine, int nMaxKeyLimit,
				boolean bWeightOut);
		public int NLPIR_AddUserWord(String sWord);//add by qp 2008.11.10
		public int NLPIR_DelUsrWord(String sWord);//add by qp 2008.11.10
		public String NLPIR_GetLastErrorMsg();
		public void NLPIR_Exit();
	}

	public static String transString(String aidString, String ori_encoding,
			String new_encoding) {
		try {
			return new String(aidString.getBytes(ori_encoding), new_encoding);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	
	public static void NLPIR_Init() {
		String argu = "";
		// String system_charset = "GBK";//GBK----0
		String system_charset = "UTF-8";
		int charset_type = 1;
		int init_flag = CLibrary.Instance.NLPIR_Init(argu, charset_type, "0");
		if (0 == init_flag) {
			String ErrorMsg = CLibrary.Instance.NLPIR_GetLastErrorMsg();
			System.err.println("初始化失败！fail reason is "+ErrorMsg);
			return;
		}
	}
	
	public static void Add_UserDictionary() throws IOException {
		long startMili=System.currentTimeMillis();// 当前时间对应的毫秒数
		BufferedReader reader = Function.ReadResult("520Data/520KeywordList.txt");
		String line = reader.readLine();
		int num = 0;
		while(line != null){
			if(line.length()>4){//暂时只使用长度大于4的关键词，以筛选大部分太短或重要性不高的关键词
				num++;
				CLibrary.Instance.NLPIR_AddUserWord(line.trim()+" keyword");
			}
			line = reader.readLine();
		}
		long endMili=System.currentTimeMillis();
		Function.println("共加入"+num+"个用户自定义关键词");
		System.out.println("总耗时为："+(endMili-startMili)+"毫秒");
	}
	
	public static void main(String[] args) throws Exception {
		NLPIR_Init();//初始化NLPIR
		Add_UserDictionary();
		String nativeBytes = null;
		long startMili=System.currentTimeMillis();// 开始时间对应的毫秒数
		BufferedReader reader = Function.ReadResult("520Data/520PaperName.txt");
		String line = reader.readLine();
		String[] spiltStrings = null;
		int num2 = 0;
		while(line != null){
			spiltStrings = line.split("\t");
			nativeBytes = CLibrary.Instance.NLPIR_ParagraphProcess(spiltStrings[1], 1);
			Function.WriteResult(nativeBytes+"\r\n", "520Data/520SegPapaerName_V1.txt",true);
			num2++;
			line = reader.readLine();
		}
		Function.println("共分词"+num2+"个");
		long endMili=System.currentTimeMillis();//结束时间的毫秒数
		System.out.println("总耗时为："+(endMili-startMili)+"毫秒");
		CLibrary.Instance.NLPIR_Exit();//关闭NLPIR包，释放内存
		
//		String sInput = "AES加密算法据悉，质检总局已将最新有关情况再次通报美方，要求美方加强对输华玉米的产地来源、运输及仓储等环节的管控措施，有效避免输华玉米被未经我国农业部安全评估并批准的转基因品系污染。";
//		try {
//			nativeBytes = CLibrary.Instance.NLPIR_ParagraphProcess(sInput, 1);
//			System.out.println("分词结果为： " + nativeBytes);
//			CLibrary.Instance.NLPIR_AddUserWord("要求美方加强对输 n");
//			CLibrary.Instance.NLPIR_AddUserWord("华玉米的产地来源 n");
//			nativeBytes = CLibrary.Instance.NLPIR_ParagraphProcess(sInput, 1);
//			System.out.println("增加用户词典后分词结果为： " + nativeBytes);
//			CLibrary.Instance.NLPIR_DelUsrWord("要求美方加强对输");
//			nativeBytes = CLibrary.Instance.NLPIR_ParagraphProcess(sInput, 1);
//			System.out.println("删除用户词典后分词结果为： " + nativeBytes);
//			
//			
//			int nCountKey = 0;
//			String nativeByte = CLibrary.Instance.NLPIR_GetKeyWords(sInput, 10,false);
//
//			System.out.print("关键词提取结果是：" + nativeByte);
//
//			nativeByte = CLibrary.Instance.NLPIR_GetFileKeyWords("D:\\NLPIR\\feedback\\huawei\\5341\\5341\\产经广场\\2012\\5\\16766.txt", 10,false);
//
//			System.out.print("关键词提取结果是：" + nativeByte);
//
//			
//
//			CLibrary.Instance.NLPIR_Exit();
//
//		} catch (Exception ex) {
//			// TODO Auto-generated catch block
//			ex.printStackTrace();
//		}

	}
}
